<?php
/**
 * Eren Framework
 * 
 * Curl
 * 
 * @category System
 * @author Manu Soros Leu
 * @copyright manusoros@gmail.com
 */

class Eren_Curl
{
    /**
     * Request web
     * 
     * @param string $URL
     * @param array $GET
     * @param array $POST
     * @param string $COOKIE
     * @throws Eren_Exception
     * 
     * @return string
     */
    static public function request($URL, $GET = NULL, $POST = NULL, $COOKIE = NULL, $timeOut = 30)
    {
        if($URL == "" || !is_string($URL)){
            throw new Eren_Exception("The first param must be String and can't be NULL");
        }
        
        if($GET!=NULL && !is_array($GET)){
            throw new Eren_Exception("The second param must be Array or NULL");
        }
        
        if($POST!=NULL && !is_array($POST)){
            throw new Eren_Exception("The third param must be Array or NULL");
        }
        
        if($COOKIE!=NULL && !is_string($COOKIE)){
            throw new Eren_Exception("The third param must be string or NULL");
        }
        
        if(stripos($URL, "http://") === FALSE){
            $URL = "http://{$URL}";
        }
        
        if($GET != NULL){
            $get_str = "";
            foreach($GET as $k=>$v){
                $v = urlencode($v);
                $get_str .= "{$k}={$v}&";
            }
            $get_str = trim($get_str, "&");
            $URL = "{$URL}?{$get_str}";
        }
        $ch = curl_init();
        if($COOKIE != NULL){
            curl_setopt($ch, CURLOPT_COOKIEJAR, $COOKIE);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $COOKIE);
        } 
        curl_setopt($ch, CURLOPT_URL, $URL);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        if($POST != NULL){
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $POST);
        }
        $output = curl_exec($ch);
        $info = curl_getinfo($ch);
        $errMsg = curl_error($ch);
        $errNo = curl_errno($ch);
        if($errNo != 0 || (isset($info['http_code']) && $info['http_code'] != 200)){
            throw new Eren_Exception("Request error, [ErrNo:{$errNo}][ErrMsg:{$errMsg}][HttpCode:{$info['http_code']}][URL:{$URL}]");
        }
        curl_close($ch);
        return $output;
    }
    
    /**
     * Wget file
     * 
     * @param string $URL
     * @param array $local
     * @throws Eren_Exception
     * 
     * @return bool
     */
    static public function wget($URL, $fileName, $local, $log="./wget.log", $user = NULL, $pass = NULL)
    {
        if($URL == "" || !is_string($URL)){
            throw new Eren_Exception("The first param must be String and can't be NULL");
        }
        
        if($local == "" || !is_string($local)){
            throw new Eren_Exception("The second param must be String and can't be NULL");
        }
        
        if(!file_exists($local) || !is_dir($local)){
            throw new Eren_Exception("The folder[{$local}] is not exist or is not a folder");
        }
        
        if(stripos($URL, "http://") === FALSE){
            $URL = "http://{$URL}";
        }
        
        $URL = $URL . "/{$fileName}";
        
        if(file_exists("{$local}/{$fileName}")){
            Eren_File::delete("{$local}/{$fileName}");
        }
        
        $cmd = "wget -P {$local} {$URL} -a {$log}";
        if($user != NULL && $pass != NULL){
            $cmd = $cmd . " --user={$user} --password='{$pass}'";
        }
        exec($cmd);
        
        if(!file_exists("{$local}/{$fileName}")){
            throw new Eren_Exception("wget [$cmd] error");
        }
        
        return TRUE;
    }
    
    /**
     * Send mail to
     * 
     * @param string $URL
     * @param array $local
     * @throws Eren_Exception
     * 
     * @return bool
     */
    static public function mail($to, $title, $content, $from)
    {
        if((is_string($to) && $to == "") || (is_array($to) && empty($to))){
            throw new Eren_Exception("The first param can't be NULL or empty");
        }
        
        if($title == "" || !is_string($title)){
            throw new Eren_Exception("The second param must be String and can't be NULL");
        }
        
        if($content == "" || !is_string($content)){
            throw new Eren_Exception("The third param must be String and can't be NULL");
        }
        
        if($from == "" || !is_string($from)){
            throw new Eren_Exception("The third param must be String and can't be NULL");
        }
        
        if(is_array($to)){
            $to = implode(", ", $to);
        }
        
        return mail($to, $title, $content, "From: {$from}");
    }
}
